﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using System.Security.Cryptography;
using WFNetLib.PacketProc;
using WFNetLib.TCP;
using System.Net.Sockets;
using WFNetLib.Log;
using System.Threading;

namespace 起落服务测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            QiLuoService.QiLuoWebService ql = new QiLuoService.QiLuoWebService();
            MessageBox.Show(ql.getDownUp(int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text)));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            const string VerifyVersion = "wfHXQL";
            SoftVerifyData svd;
            try
            {
                svd = SoftVerify.GetVerifyData(VerifyVersion);
            }
            catch (CryptographicException ex)//数据被更改
            {
                ShowText("系统关键数据被更改:" + ex.Message);
                return;// false.ToString();
            }
            if (svd.UsedDays > 2)
            {
                ShowText("系统关键数据1超限");
                return;// false.ToString();
            }
            else
            {
                DateTime cur = DateTime.Now;
                if (svd.CurTime.AddDays(1) < DateTime.Now || svd.CurTime > DateTime.Now)
                {
                    svd.UsedDays++;
                    svd.CurTime = DateTime.Now;
                }
            }
            if (svd.UsedTimes > 50000)
            {
                ShowText("系统关键数据2超限");
                return;// false.ToString();
            }
            else
            {
                svd.UsedTimes++;
            }
            SoftVerify.SetVerifyData(VerifyVersion, svd);
        }
//         void ShowText(string str)
//         {
//             MessageBox.Show(str);
//         }

        private void button3_Click(object sender, EventArgs e)
        {
            mm(int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text));
        }
        string mm(int channel, int inout, int tailuo)
        {
            byte[] tx = new byte[2 + 5];
            tx[0] = 0x00;//起始地址
            if (inout == 1 || inout == 2)//入口
            {
                if (tailuo == 0)//抬起
                    tx[1] = 0xe1;
                else
                    tx[1] = 0xe2;
            }
            else//出口
            {
                if (tailuo == 0)//抬起
                    tx[1] = 0xe3;
                else
                    tx[1] = 0xe4;
            }
            tx[2] = 0;//长度
            tx[3] = 1;
            tx[4] = 2;//写入字节数
            channel--;
            ushort c = (ushort)(1 << channel);
            tx[5] = BytesOP.GetHighByte(c);
            tx[6] = BytesOP.GetLowByte(c);
            int retry;
            retry = 5;
            while (retry != 0)
            {
                TcpModbusPacket tp = ModbusWork(tx, TcpModbusPacket.FunctionCode.Write);
                if (tp == null)
                {
                    ShowText("闸杆数据未接收到PLC数据");
                }
                else
                    break;
                Thread.Sleep(1000);
                retry--;
            }
            if (retry == 0)
                return false.ToString();
            Thread.Sleep(5000);
            tx[5] = 0;
            tx[6] = 0;
            retry = 5;
            while (retry != 0)
            {
                TcpModbusPacket tp = ModbusWork(tx, TcpModbusPacket.FunctionCode.Write);
                if (tp == null)
                {
                    ShowText("闸杆数据未接收到PLC数据");
                }
                else
                    break;
                Thread.Sleep(1000);
                retry--;
            }
            if (retry == 0)
                return false.ToString();
            return true.ToString();
        }
        TcpModbusPacket ModbusWork(byte[] tx, TcpModbusPacket.FunctionCode type)
        {
            string str;
            TCPSyncClient tcp = new TCPSyncClient();
            tcp.TCPServerName = "10.103.4.106";
            //tcp.TCPServerName = "127.0.0.1";
            tcp.SaveDataProcessCallback = new SaveDataProcessCallbackDelegate(TcpModbusPacket.SaveDataProcessCallbackProc);
            tcp.TCPServerPort = 502;
            //ShowText("-----------------------"+DateTime.Now.ToLocalTime()+"------------------------");
            if (!tcp.Conn())
            {
                str = "无法连接到服务器:";
                if (tcp.LastException.GetType() == typeof(SocketException))
                {
                    SocketException ex = (SocketException)tcp.LastException;
                    switch (ex.ErrorCode)
                    {
                        case 10061:
                            str += ex.Message;
                            break;
                        case 10060:
                            str += ex.Message;
                            break;
                    }
                }
                else
                    str = "未知错误:" + tcp.LastException.Message;
                ShowText(str);
                return null;
            }
            ShowText("发送数据:" + WFNetLib.Strings.StringsFunction.byteToHexStr(tx, " "));
            byte[] txBytes = TcpModbusPacket.MakePacket(0, 0, type, tx);
            if (!tcp.SendPacket(txBytes))
            {
                return null;
            }
            object o = tcp.ReceivePacket();
            if (o == null)
            {
                ShowText("未收到需要读取的数据");
                return null;
            }
            TcpModbusPacket tp = (TcpModbusPacket)o;
            ShowText("收到数据:" + WFNetLib.Strings.StringsFunction.byteToHexStr(tp.Data, " "));
            tcp.Close();
            return tp;
        }
        public void ShowText(string str)
        {
            DateTime dt = DateTime.Now;
            TextLog.AddTextLog(str, "D:\\起落控制服务日志\\" + String.Format("{0:D4}", dt.Year) + "-" + String.Format("{0:D2}", dt.Month) + "-" + String.Format("{0:D2}", dt.Day) + "运行信息.txt", false);
        }
    }
}
